﻿using Apache.NMS;
using Apache.NMS.ActiveMQ;
using Apache.NMS.Util;
using DBEntitie;
using FrameWork.Extensions;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
using QRCoder;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Xml;

namespace FrameWork
{
   public static class PublicTool
   {
        private static readonly DateTime Maminitime = DateTime.Parse("1753-01-01 12:00:00");
        private static readonly string BaseDirectory = AppDomain.CurrentDomain.BaseDirectory + "\\Upload\\";
       public static string ApplicationHost
       {
            get
            {
                var meathos = TimHttpContext.Context.Request.IsHttps ? "https://" : "http://";
                var host = TimHttpContext.Context.Request.Host;
                var hostStr = host.Port == null || host.Port == 80 || host.Port == 443 ? host.Host : $"{host.Host}:{host.Port}";
                return $"{meathos}{hostStr}/";
            }
        }
       public class ComResult<T>
       {
           public int code { get; set; }
#pragma warning restore CS1591 // 缺少对公共可见类型或成员“PublicTool.ComResult<T>.code”的 XML 注释
           public T data { get; set; }
       }
       public static string NewGuid(bool format = false)
       {
            return format ? Guid.NewGuid().ToString().Replace("-", "") : Guid.NewGuid().ToString();
        }
       public static int Random(int min, int max)
       {
            return new Random(NewGuid().GetHashCode()).Next(min, max);
        }
       public static string GetOrder(long userindex, int length = 32)
       {
            var oreder = $"{userindex}{DateTime.Now:yyyyMMddHHmmssfff}{Random(1000, 10000)}{NewGuid().GetHashCode()}".Replace("-", "");
            return oreder.Substring(0, length);
        }
       public static byte[] QrCode(string strCode)
       {
            using (var ms = new MemoryStream())
            {
                try
                {
                    var qrGenerator = new QRCodeGenerator();
                    var qrCodeData = qrGenerator.CreateQrCode(strCode, QRCodeGenerator.ECCLevel.Q);
                    var qrcode = new QRCode(qrCodeData);

                    var qrCodeImage = qrcode.GetGraphic(5, Color.Black, Color.White, null, 15, 6, false);
                    qrCodeImage.Save(ms, ImageFormat.Jpeg);
                }
                catch (Exception)
                {
                    // ignored
                }
                return ms.ToArray();
            }
        }
        private static async Task<JsonResult> DicToJson<T>(T obj, int code)
        {
            var res = new ComResult<T>
            {
                code = code,
                data = obj
            };

            var setting = new JsonSerializerSettings
            {
                DateFormatHandling = DateFormatHandling.MicrosoftDateFormat,
                DateFormatString = "yyyy-MM-dd HH:mm:ss",
                //ContractResolver = new CamelCasePropertyNamesContractResolver(),
                Formatting = Newtonsoft.Json.Formatting.Indented
            };
            var json = new JsonResult(res, setting)
            {
                ContentType = "application/json; charset=utf-8",
                StatusCode = 200,
                Value = res
            };
            return await Task.Run(() => json);
        }
       public static SortedDictionary<string, object> XmlToDic(string xml)
       {
            if (string.IsNullOrWhiteSpace(xml)) return null;
            try
            {
                var paramdic = new SortedDictionary<string, object>();
                var xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(xml);
                var xmlNode = xmlDoc.FirstChild;
                var nodes = xmlNode.ChildNodes;
                var stringBuilder = new StringBuilder();
                foreach (XmlNode xn in nodes)
                {
                    var xe = (XmlElement)xn;
                    paramdic[xe.Name] = xe.InnerText;
                    stringBuilder.Append($"{xe.Name}={xe.InnerText}&");
                }
                //Log4Help.Debug("Xml->" + stringBuilder);
                return paramdic;
            }
            catch (Exception)
            {
                return null;
            }
        }
       public static string DicToXml(SortedDictionary<string, object> param)
       {
            if (param == null || param.Count == 0) return null;

            var stringBuilder = new StringBuilder();

            stringBuilder.Append("<xml>");
            foreach (var itm in param)
            {
                if (string.IsNullOrWhiteSpace(itm.Value?.ToString()) || string.IsNullOrWhiteSpace(itm.Key)) continue;

                if (itm.Value is int)
                {
                    stringBuilder.Append($"<{itm.Key}>{itm.Value}</{itm.Key}>");
                }

                if (itm.Value is string)
                {
                    stringBuilder.Append($"<{itm.Key}><![CDATA[{itm.Value}]]></{itm.Key}>");
                }
            }
            stringBuilder.Append("</xml>");
            //Log4Help.Debug("Xml->" + stringBuilder);
            return stringBuilder.ToString();
        }

       public static string ParamToUrl(SortedDictionary<string, object> paramdic, bool paramserial = false)
       {
            return paramserial ? paramdic.Aggregate("", (current, itm) => current + $"{System.Web.HttpUtility.UrlEncode(itm.Key)}={System.Web.HttpUtility.UrlEncode(itm.Value.ToString())}&").TrimEnd('&') : paramdic.Aggregate("", (current, itm) => current + $"{itm.Key}={itm.Value.ToString()}&").TrimEnd('&');
        }
       public static bool UrlToParam(string url, out Dictionary<string, string> paramdic)
       {
            paramdic = new Dictionary<string, string>();
            if (string.IsNullOrWhiteSpace(url)) return false;
            url = url.Contains("?") ? url.Split('?')[1] : url;

            try
            {
                paramdic = url.Split('&').Select(itm => itm.Split('=')).Where(itmsplit => itmsplit.Length == 2).ToDictionary(itmsplit => System.Web.HttpUtility.UrlDecode(itmsplit[0] ?? ""), itmsplit => System.Web.HttpUtility.UrlDecode(itmsplit[1] ?? ""));
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
       public static async Task<JsonResult> ReturnSuccess<T>(T obj, int code = 0)
       {
            return await DicToJson(obj, code);
        }

       public static async Task<JsonResult> ReturnError<T>(T obj, int code = -1)
       {
            return await DicToJson(obj, code);
        }
       public static async Task<ContentResult> ReturnSuccessJsonp(object obj, int code = 0)
       {
            return await ReturnJsonp(obj, code);
        }

       public static async Task<ContentResult> ReturnErrorJsonp(object obj, int code = -1)
       {
            return await ReturnJsonp(obj, code);
        }
        private static async Task<ContentResult> ReturnJsonp<T>(T obj, int code = 0)
        {
            var json = JsonResultToStr(await DicToJson(obj, code));

            JsonpAction(out var callback);

            return await Task.Run(() => new ContentResult
            {
                Content = $"{callback}({json})",
                ContentType = "text/plain",
                StatusCode = 200
            });
        }
       public static bool JsonpAction(out string callback)
       {
            callback = "";
            TimHttpContext.Context.Request.Query.TryGetValue("callback", out var callbacks);
            callback = callbacks.FirstOrDefault() ?? "callback";
            return !string.IsNullOrWhiteSpace(callbacks.FirstOrDefault());
        }
       public static async Task<ContentResult> ReturnXml(bool state)
       {
            var returnCode = state ? "SUCCESS" : "FAIL";

            var sortedDictionary = new SortedDictionary<string, object>
            {
                { "return_code",returnCode}
            };

            return await Task.Run(() => new ContentResult
            {
                Content = DicToXml(sortedDictionary),
                ContentType = "text/plain",
                StatusCode = 200
            });
        }
       public static string JsonResultToStr(JsonResult json)
       {
            return JObject.FromObject(json.Value).ToString();
        }
       public static async Task<string> ReturnString(string data)
       {
            return await Task.Run(() => data);
        }
        private static bool ValidateChineseString(string value)
        {
            if (string.IsNullOrWhiteSpace(value)) return false;

            const string regstr = "^[\\u4e00-\\u9fa5]{0,}$";

            return Regex.IsMatch(value, regstr);
        }
       public static void ValidateModel(ModelStateDictionary modelState, string[] validatelist = null)
       {
            if (modelState.IsValid) return;

            foreach (var itm in modelState.Keys)
            {
                foreach (var error in modelState[itm].Errors)
                {
                    if (string.IsNullOrWhiteSpace(error.ErrorMessage)) continue;

                    if (validatelist != null && !validatelist.Contains(itm)) continue;
                    var errorMessage = error.ErrorMessage;

                    if (ValidateChineseString(errorMessage)) throw new IException($"{errorMessage}");

                    throw new IException($"参数错误:{itm}");
                }
            }
        }

       public static IEnumerable<string> SaveFiles(HttpRequest request, string keys = null, string[] fileType = null, bool alownull = false)
       {
            var filelist = GetFiles(request, keys, fileType, alownull);

            if (filelist != null && filelist.Any()) return SaveFiles(filelist);

            if (alownull) return new List<string>();

            throw new IException("保存文件为空,请检查");
        }

       public static List<string> SaveFiles(IReadOnlyCollection<KeyValuePair<string, byte[]>> filelist, string savepath = null, bool withpath = false)
       {
            var fileNames = new List<string>();
            if (filelist == null || !filelist.Any()) return fileNames;
            savepath = string.IsNullOrWhiteSpace(savepath) ? BaseDirectory : savepath;

            var yunUse = ConfigurationManager.AppSettings("yun_use");
            bool.TryParse(yunUse, out var yunuse);
            if (!Directory.Exists(savepath)) Directory.CreateDirectory(savepath);
            foreach (var itm in filelist)
            {
                var filename = $"{DateTime.Now:yyyyMMddHHmmssfff}{TimRandom(5, true, true)}{Random(1000, 10000)}{itm.Key}";
                if (yunuse)
                {
                    YunUpload(filename, itm.Value);
                    continue;
                }
                using (var fs = new FileStream(savepath + filename, FileMode.Create, FileAccess.Write))
                {
                    fs.Write(itm.Value, 0, itm.Value.Length);
                }
                fileNames.Add(withpath ? savepath + filename : filename);
            }
            return fileNames;
        }

        private static void YunUpload(string filename, byte[] byteArray, string savepath = "/img/")
        {
            var bucketname = ConfigurationManager.AppSettings("yun_bucketname");
            var username = ConfigurationManager.AppSettings("yun_username");
            var password = ConfigurationManager.AppSettings("yun_password");
            try
            {
                //                var resault = new UpYun(bucketname, username, password).WriteFile(savepath + filename, byteArray, true);
                //                if (!resault) throw new IException("图片上传失败,请稍后重试");
            }
            catch (Exception ex)
            {
                Log4Help.Error("又拍云上传失败", ex);
                throw new IException("图片上传失败,请稍后重试");
            }
        }
       public static DateTime ConvertStartTime(string value)
       {
            if (string.IsNullOrWhiteSpace(value))
            {
                throw new IException("请选择开始时间");
            }
            if (DateTime.TryParse(value, out var time)) return time;

            var batch = FormatStr(value.Replace("-", "").Replace("\\", "").Replace("年", "").Replace("月", "").Replace("日", ""));

            return batch.Length == 6 ? DataTimeParse(batch + "01", 6) : DataTimeParse(batch);
        }
       public static DateTime ConvertEndTime(string value)
       {
            if (string.IsNullOrWhiteSpace(value))
            {
                throw new IException("请选择结束时间");
            }
            if (DateTime.TryParse(value, out var time)) return time.AddDays(1);

            var batch = FormatStr(value.Replace("-", "").Replace("\\", "").Replace("年", "").Replace("月", "").Replace("日", ""));

            return batch.Length == 6 ? DataTimeParse(batch + "01", 6).AddMonths(1) : DataTimeParse(batch).AddDays(1);
        }

       public static bool TimeIsMonth(string value)
       {
            if (string.IsNullOrWhiteSpace(value))
            {
                return false;
            }

            var batch = FormatStr(value.Replace("-", "").Replace("\\", "").Replace("年", "").Replace("月", "").Replace("日", ""));

            return batch.Length == 6;
        }

       public static DateTime DataTimeParse(string value, int length = 8)
       {
            if (string.IsNullOrWhiteSpace(value))
            {
                return Maminitime;
            }
            value = FormatStr(value);

            if (value.Equals("0000-00-00 00:00:00") || value.Length < length)
            {
                return Maminitime;
            }

            try
            {
                var time = DateTime.Parse(value);
                return time < Maminitime ? Maminitime : time;
            }
            catch (Exception)
            {
                // ignored
            }

            var year = value.Substring(0, 4);
            var month = value.Substring(4, 2);
            var day = value.Substring(6, 2);
            var hour = "00";
            var minute = "00";
            var second = "00";

            if (int.Parse(month) > 12 || int.Parse(day) > 31)
            {
                return Maminitime;
            }

            if (!value.Contains(" "))
            {
                return DateTime.Parse(year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second);
            }
            var timevalve = value.Split(' ')[1];

            if (timevalve.Length >= 2)
            {
                hour = timevalve.Substring(0, 2);
            }

            if (timevalve.Length >= 4)
            {
                minute = timevalve.Substring(3, 2);
            }

            if (timevalve.Length >= 6)
            {
                second = timevalve.Substring(5, 2);
            }

            hour = int.Parse(hour) > 23 ? "00" : hour;
            minute = int.Parse(minute) > 59 ? "00" : minute;
            second = int.Parse(second) > 59 ? "00" : second;

            return DateTime.Parse(year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second);
        }
       public static string FormatStr(string value)
       {
            if (value == null) return "";

            var revalue =
                value.TrimStart('"')
                    .TrimEnd('"')
                    .TrimStart(',')
                    .TrimEnd(',')
                    .TrimStart('\'')
                    .TrimEnd('\'')
                    .TrimStart(' ')
                    .TrimEnd(' ')
                    .TrimStart('\r')
                    .TrimEnd('\r')
                    .TrimStart('\n')
                    .TrimEnd('\n');

            return revalue;
        }
       public static void MqSend(object message, string strTopicName, string host = null, string port = null)
       {
            var mqhost = string.IsNullOrWhiteSpace(host) ? ConfigurationManager.AppSettings("ACTIVEMQ_HOST") : host;
            var mqport = int.Parse(string.IsNullOrWhiteSpace(port) ? ConfigurationManager.AppSettings("ACTIVEMQ_PORT") : port);

            var factory = new ConnectionFactory($"tcp://{mqhost}:{mqport}/");

            using (var connection = factory.CreateConnection())
            {
                using (var session = connection.CreateSession())
                {
                    var destination = SessionUtil.GetDestination(session, strTopicName, DestinationType.Queue);

                    var prod = session.CreateProducer(destination);

                    var msg = prod.CreateObjectMessage(message);

                    prod.Send(msg, MsgDeliveryMode.Persistent, MsgPriority.Normal, TimeSpan.MinValue);
                }
            }
        }
       public static bool IsInt(string value)
       {
            return Regex.IsMatch(value, @"^\d*$");
        }
       public static string GetTimeStamp()
       {
            var ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return (Convert.ToInt64(ts.TotalSeconds) + 10000).ToString();
        }
       public static DateTime ConvertTimestamp(long timestamp)
       {
            var converted = new DateTime(1970, 1, 1, 0, 0, 0, 0);
            var newDateTime = converted.AddMilliseconds(timestamp);
            return newDateTime.ToLocalTime();
        }

       public static List<KeyValuePair<string, byte[]>> GetFiles(HttpRequest request, string keys = null, string[] fileType = null, bool alownull = false)
       {
            var files = request.Form.Files;
            var filelist = new List<KeyValuePair<string, byte[]>>();
            const int maxsize = 100000 * 1024;

            if (alownull && files.Count == 0) return filelist;
            if (files.Count == 0) throw new IException("基础上传文件为空,请检查");

            foreach (var file in files)
            {
                if (!string.IsNullOrWhiteSpace(keys) && file?.Name != keys) continue;

                if (alownull && (file == null || file.Length <= 0)) return filelist;

                if (file == null || file.Length <= 0) throw new IException($"{keys ?? "指定文件名"}文件为空,请检查");

                var filename = Path.GetFileName(file.FileName);
                var filesize = file.Length;
                var extension = Path.GetExtension(filename);
                if (string.IsNullOrWhiteSpace(extension)) throw new IException("获取文件扩展名失败");
                var fileEx = extension.ToLower();

                if (fileType != null && !fileType.Contains(fileEx)) throw new IException($"文件类型错误，只能导入{string.Join(",", fileType)}格式的文件");

                if (filesize > maxsize) throw new IException("文件大小超过100M,上传失败");

                var stream = file.OpenReadStream();

                var bytes = new byte[stream.Length];
                stream.Read(bytes, 0, bytes.Length);
                stream.Seek(0, SeekOrigin.Begin);
                filelist.Add(new KeyValuePair<string, byte[]>(fileEx, bytes));
            }
            return filelist;
        }
       public static Image LinkToImage(string link)
       {
            if (string.IsNullOrWhiteSpace(link)) return null;

            try
            {
                var stream = WebRequest.Create(link).GetResponse().GetResponseStream();
                if (stream == null) return null;
                var result = Image.FromStream(stream);
                stream.Close();
                return result;
            }
            catch (Exception)
            {
                return null;
            }
        }

       public static string ByteToBase64String(byte[] data)
       {
            try
            {
                return $"data:image/png;base64,{Convert.ToBase64String(data)}";
            }
            catch (Exception)
            {
                return null;
            }
        }

       public static string ImageToBase64String(Image bitmap)
       {
            try
            {
                var ms = new MemoryStream();
                bitmap.Save(ms, bitmap.RawFormat);
                var byteImage = ms.ToArray();

                return ByteToBase64String(byteImage);
            }
            catch (Exception)
            {
                return null;
            }
        }
       public static string TimRandom(int length, bool hasnum = true, bool hasEnglish = false)
       {
            var random = "";
            var randomlist = new List<string>();
            if (hasnum)
            {
                for (var i = 0; i <= 9; i++)
                {
                    randomlist.Add(i.ToString());
                }
            }

            if (hasEnglish)
            {
                for (var i = 'A'; i <= 'F'; i++)
                {
                    randomlist.Add(i.ToString());
                }
            }

            for (var i = 0; i < length; i++)
            {
                random += randomlist[new Random(NewGuid().GetHashCode()).Next(randomlist.Count)];
            }
            return random;
        }
       public static byte[] DecodeBase64ToByte(string result)
       {
            try
            {
                return Convert.FromBase64String(result);
            }
            catch
            {
                return null;
            }
        }
       public static string EcodeBase64(byte[] result)
       {
            try
            {
                return Convert.ToBase64String(result);
            }
            catch (Exception)
            {
                return null;
            }
        }
       public static string GetString(byte[] soure)
       {
            try
            {
                return Encoding.UTF8.GetString(soure);
            }
            catch (Exception)
            {
                return null;
            }
        }
       public static byte[] GetBytes(string soure)
       {
            try
            {
                return Encoding.UTF8.GetBytes(soure);
            }
            catch (Exception)
            {
                return null;
            }
        }
       public static string DecodeBase64(Encoding encode, string result)
       {
            try
            {
                var bytes = DecodeBase64ToByte(result);
                return encode.GetString(bytes);
            }
            catch (Exception)
            {
                return null;
            }
        }
       public static List<T> DeserializeObject<T>(string source)
       {
            try
            {
                return JsonConvert.DeserializeObject<List<T>>(source) ?? new List<T>();
            }
            catch (Exception)
            {
                return new List<T>();
            }
        }
       public static string SerializeObject(object obj)
       {
            try
            {
                return JsonConvert.SerializeObject(obj);
            }
            catch (Exception)
            {
                return null;
            }
        }
       public static string Decrypt(string decryptStr, string key)
       {
            try
            {
                var keyArray = Encoding.UTF8.GetBytes(key);
                var toEncryptArray = Convert.FromBase64String(decryptStr);
                var rDel = new RijndaelManaged
                {
                    Key = keyArray,
                    Mode = CipherMode.ECB,
                    Padding = PaddingMode.PKCS7
                };
                var cTransform = rDel.CreateDecryptor();
                var resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
                return Encoding.UTF8.GetString(resultArray);
            }
            catch (Exception)
            {
                return null;
            }
        }
       public static Image Base64StringToImage(string base64String)
       {
            try
            {
                base64String = base64String.Replace("data:img/jpg;base64,", "");
                var bytes = Convert.FromBase64String(base64String);
                var stream = new MemoryStream(bytes);
                return new Bitmap(new Bitmap(stream));
            }
            catch (Exception)
            {
                // ignored
                return null;
            }
        }
       public static string Md5Encryption(string myString)
       {
            using (var md5 = MD5.Create())
            {
                var result = md5.ComputeHash(Encoding.UTF8.GetBytes(myString));
                var strResult = BitConverter.ToString(result);
                return strResult.Replace("-", "");
            }
        }

       public static byte[] StreamToBytes(Stream stream)
       {
            var outstream = new MemoryStream();
            const int bufferLen = 1;
            var buffer = new byte[bufferLen];
            var count = 0;
            while ((count = stream.Read(buffer, 0, bufferLen)) > 0)
            {
                outstream.Write(buffer, 0, count);
            }

            outstream.Seek(0, SeekOrigin.Begin);
            var bytes = new byte[outstream.Length];
            outstream.Read(bytes, 0, bytes.Length);
            outstream.Flush();
            outstream.Close();
            if (stream.CanSeek)
            {
                stream.Seek(0, SeekOrigin.Begin);
            }
            else
            {
                stream.Close();
            }
            return bytes;
        }
      

       public static Stream BytesToStream(byte[] bytes)
       {
            Stream stream = new MemoryStream(bytes);
            return stream;
        }
       public static byte[] FileToByte(string filename, string filepath = null)
       {
            filepath = string.IsNullOrWhiteSpace(filepath) ? BaseDirectory : filepath;
            if (!Directory.Exists(filepath)) Directory.CreateDirectory(filepath);
            try
            {
                using (var read = new FileInfo(Path.Combine(filepath, filename)).OpenRead())
                {
                    var buffer = new byte[read.Length];
                    read.Read(buffer, 0, Convert.ToInt32(read.Length));
                    read.Close();
                    return buffer;
                }
            }
            catch (Exception)
            {
                return null;
            }
        }

       public static string ReadFile(string filename, string filepath = null)
       {
            return Encoding.UTF8.GetString(FileToByte(filename, filepath) ?? new byte[0]);
        }

       public static bool GetTimeNow(out DateTime timeNow)
       {
            timeNow = DateTime.Now;

            var content = new TimHttpContent
            {
                CookieData = new CookieCollection(),
                Header = new Dictionary<string, string>(),
                Method = TimHttp.Method.Get,
                Url = "http://api.m.taobao.com/rest/api3.do?api=mtop.common.getTimestamp",
            };
            var html = new TimHttp().PostOrGet(ref content);
            var json = html.TryParse();
            long.TryParse(json?["data"]?["t"]?.ToString() ?? "0", out var timeNum);

            timeNow = ConvertTimestamp(timeNum);
            return timeNum != 0;
        }

       public static string WriteFile(string filename, byte[] bytes, string filepath = null)
       {
            filepath = string.IsNullOrWhiteSpace(filepath) ? BaseDirectory : filepath;
            if (!Directory.Exists(filepath)) Directory.CreateDirectory(filepath);
            var realfile = Path.Combine(filepath, filename);
            try
            {
                using (var file = new FileStream(realfile, FileMode.OpenOrCreate, FileAccess.Write))
                {
                    file.Write(bytes, 0, bytes.Length);
                    file.Close();
                }
            }
            catch (Exception)
            {
                realfile = null;
            }
            return realfile;
        }
       public static string WriteFile(string filename, Stream stream, string filepath = null)
       {
            return WriteFile(filename, StreamToBytes(stream), filepath);
        }
       public static string WriteFile(string filename, string str, string filepath = null)
       {
            return WriteFile(filename, Encoding.UTF8.GetBytes(str), filepath);
        }

       public static bool LastVersion(int platform, string version, out string lastVersion)
       {
            lastVersion = "";

            var versionPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Version");
            if (!Directory.Exists(versionPath)) Directory.CreateDirectory(versionPath);

            try
            {
                var nowVersion = new Version(version);
                var maxVersion = new Version(version);

                foreach (var item in new DirectoryInfo(versionPath).GetFiles())
                {
                    var itemVersion = new Version(Path.GetFileNameWithoutExtension(item.FullName));

                    if (platform == 1)
                    {
                        if (item.Extension != ".apk") continue;
                        if (itemVersion <= maxVersion) continue;
                        maxVersion = itemVersion;
                    }

                    if (platform == 2)
                    {
                        if (item.Extension != ".ipa") continue;
                        if (itemVersion <= maxVersion) continue;
                        maxVersion = itemVersion;
                    }
                }

                lastVersion = maxVersion.ToString();
                return maxVersion <= nowVersion;
            }
            catch (Exception)
            {
                return true;
            }
        }

       public static bool GetVersion(int platform, string version, out byte[] lastVersion)
       {
            lastVersion = new byte[] { };

            var versionPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Version");
            if (!Directory.Exists(versionPath)) Directory.CreateDirectory(versionPath);

            if (platform == 1)
            {
                lastVersion = FileToByte($"{version}.apk", versionPath);
            }

            if (platform == 2)
            {
                lastVersion = FileToByte($"{version}.apk", versionPath);
            }

            return lastVersion != null;
        }

       public static void Debug(string message)
       {
            var ip = UserInfo.Ip;

            Log4Help.Debug($"IP:{ip},{message}");
        }
       public static string left(string text,int li)
       {
            return text.Substring(0, li);
        }
       public static string right(string text,int li)
       {
            return text.Substring(text.Length - li, li);
        }
    }
}
